package cn.iocoder.yudao.module.member.controller.admin.company;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.encryption.EncryptionUtil;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.member.controller.admin.company.vo.*;
import cn.iocoder.yudao.module.member.convert.company.CompanyConvert;
import cn.iocoder.yudao.module.member.convert.company.CompanyUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.company.CompanyDO;
import cn.iocoder.yudao.module.member.dal.dataobject.company.CompanyUserDO;
import cn.iocoder.yudao.module.member.service.company.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 -运营端- 企业管理")
@RestController
@RequestMapping("/member/company")
@Validated
public class CompanyController {

    @Resource
    private CompanyService companyService;

    @PostMapping("/create")
    @ApiOperation("创建企业管理")
    public CommonResult<Long> createCompany(@Valid  @RequestBody CompanyCreateReqVO createReqVO) {
        return success(companyService.createCompany(createReqVO));
    }


    @PutMapping("/update")
    @ApiOperation("更新企业管理")
    @TenantIgnore
    public CommonResult<Boolean> updateCompany(@Valid  @RequestBody CompanyUpdateReqVO updateReqVO) {
        companyService.updateCompany(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateOrderNum")
    @ApiOperation("更新企业排序")
    @TenantIgnore
    public CommonResult<Boolean> updateOrderNum(@Valid  @RequestBody CompanyUpdateReqVO updateReqVO) {
        companyService.updateOrderNum(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除企业管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deleteCompany(@RequestParam("id") Long id) {
        companyService.deleteCompany(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得企业管理")
    @TenantIgnore
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<CompanyRespVO> getCompany(@RequestParam("id") Long id) {
        CompanyDO company = companyService.getCompany(id,0);
        return success(CompanyConvert.INSTANCE.convert(company));
    }
    @GetMapping("/oper-get")
    @ApiOperation("获得企业管理")
    @TenantIgnore
    public CommonResult<CompanyRespVO> getOperCompany(@RequestParam("id") Long id) {
        CompanyDO company = companyService.getCompany(id,0);
        return success(CompanyConvert.INSTANCE.convert(company));
    }

    @GetMapping("/list")
    @ApiOperation("获得企业管理列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<CompanyRespVO>> getCompanyList(@RequestParam("ids") Collection<Long> ids) {
        List<CompanyDO> list = companyService.getCompanyList(ids);
        return success(CompanyConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得企业管理分页")
    @TenantIgnore
    public CommonResult<PageResult<CompanyRespVO>> getCompanyPage(@Valid CompanyPageReqVO pageVO) {
        PageResult<CompanyDO> pageResult = companyService.getCompanyPage(pageVO);
        return success(CompanyConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出企业管理 Excel")
    @OperateLog(type = EXPORT)
    public void exportCompanyExcel(@Valid CompanyExportReqVO exportReqVO,
                                   HttpServletResponse response) throws IOException {
        List<CompanyDO> list = companyService.getCompanyList(exportReqVO);
        // 导出 Excel
        List<CompanyExcelVO> datas = CompanyConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "企业管理.xls", "数据", CompanyExcelVO.class, datas);
    }


    @GetMapping("/authenticationlist")
    @ApiOperation("获得认证后企业管理列表")
    @TenantIgnore
    public CommonResult<List<CompanyRespVO>> authenticationlist() {
        List<CompanyDO> list = companyService.getCompanyList();
         for (int i=0; i<list.size();i++){
             if(StringUtils.isEmpty(list.get(i).getCompanyLogo())||StringUtils.isEmpty(list.get(i).getCompanyShortName())){
                 list.remove(i);
             }
         }
        return success(CompanyConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/authenticationRecruilist")
    @ApiOperation("获得认证后企业管理列表")
    @TenantIgnore
    public CommonResult<List<CompanyRespVO>> authenticationRecruilist() {
        List<CompanyDO> list = companyService.getCompanyList();
        return success(CompanyConvert.INSTANCE.convertList(list));
    }


    @GetMapping("/admin/getCompanyLocal")
    @ApiOperation("获取企业面试地点json")
    public CommonResult<String> getCompanyLocal(@RequestParam("id") Long id) {
        CompanyDO company = companyService.getCompany(id,0);
        return success(company.getCompanyAddress());
    }

    @PostMapping("/company/authentication")
    @ApiOperation("企业认证审核")
    @TenantIgnore
        public CommonResult<Boolean> authenticationCompany(@RequestBody AuhentCationReqVo auhentCationReqVo) {
        return success(companyService.authenticationCompany(auhentCationReqVo.getId(),auhentCationReqVo.getAuthorizedStatus(),auhentCationReqVo.getRemark()));
    }

    @PostMapping("/user/page")
    @ApiOperation("获得企业管理用户分页")
    @TenantIgnore
    public CommonResult<PageResult<CompanyUserRespVO>> getCompanyUserPage(@RequestBody CompanyUserPageReqVO pageVO) {
        PageResult<CompanyUserDO> pageResult = companyService.getCompanyUserPage(pageVO);
        return success(CompanyUserConvert.INSTANCE.convertPage(pageResult));
    }


    @TenantIgnore
    @PostMapping("/user/exportExcel")
    @ApiOperation("导出企业管理 Excel")
    @OperateLog(type = EXPORT)
    public void exportExcel(@RequestBody CompanyUserPageReqVO exportReqVO,
                                   HttpServletResponse response) throws IOException {


        List<CompanyUserDO> list = companyService.getCompanyUserList(exportReqVO);
        // 导出 Excel
        List<CompanyUserExcelVO> data =new ArrayList<>();
        if(CollectionUtils.isNotEmpty(list)){
            list.stream().forEach(m->{
                CompanyUserExcelVO u=new CompanyUserExcelVO();
                u.setNickname(m.getNickname());
                u.setCompanyName(m.getCompanyName());
                u.setDept(m.getDept());
                u.setEmail(m.getEmail());
                u.setAuthorizedStatus(m.getAuthorizedStatus());
                u.setMobile(EncryptionUtil.desDecrypt(m.getPhoneNumber()));
                u.setCreateTime(m.getCreateDate());
                if(null!=m.getUnrealFlag() && m.getUnrealFlag()==1){
                    u.setUnrealFlagText("是");
                }else{
                    u.setUnrealFlagText("否");
                }
                u.setLoginDate(m.getLoginDate());
                u.setCompanyType(m.getCompanyType());
                data.add(u);
            });

        }
        ExcelUtils.write(response, "企业列表.xls", "数据", CompanyUserExcelVO.class, data);
    }

    @GetMapping("/regist/count")
    @ApiOperation("公司用户注册数量")
    public CommonResult<Integer> getCompanyRegistCount( String startDate, String endDate) {
        return success(companyService.getCompanyRegistCount( startDate, endDate ));
    }

    @PostMapping("/user/get")
    @ApiOperation("获得企业管理用户")
    @TenantIgnore
    public CommonResult<CompanyUserRespVO> getCompanyUser(@RequestBody CompanyUserReqVO companyUserReqVO) {
        CompanyUserDO pageResult = companyService.getCompanyUser(companyUserReqVO);
        return success(CompanyUserConvert.INSTANCE.convert(pageResult));
    }

    @DeleteMapping("/user/delete")
    @ApiOperation("删除企业管理用户")
    @TenantIgnore
    public CommonResult<Boolean> deleteCompanyUser(@RequestBody CompanyUserReqVO companyUserReqVO) {
        companyService.deleteCompanyUser(companyUserReqVO);
        return success(true);
    }

}
